package com.zmn.oms.business.impl.messageV1;

import com.alibaba.fastjson.JSON;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.manager.mq.interfaces.ZmnMQSender;
import com.zmn.oms.constant.MessageQueueTopicConstant;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.dto.messageV1.SendMscDIO;
import org.apache.rocketmq.common.message.MessageClientIDSetter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 类描述：
 *
 * @author yule
 * @date 2020/12/22 22:22
 */
@Component
public class ZmnMcsSendUtil {
    private static final Logger logger = LoggerFactory.getLogger(ZmnMcsSendUtil.class);

    private static ZmnMQSender sender;
    @Resource
    private ZmnMQSender zmnMQSender;

    @PostConstruct
    public void init() {
        sender = zmnMQSender;
    }

    /**
     * 发送消息工具类 发送消息唯一TAG: message_center_system
     *
     * @param sendMscDIO 消息内容
     * @return
     */
    public static String send(SendMscDIO sendMscDIO) {
        String serialNumber = MessageClientIDSetter.createUniqID();
        sendMscDIO.setSerialNumber(serialNumber);
        sendMscDIO.setCallsName("OMS");
        logger.info("#oms#开始调用发送工具开始发送队列通知topic={},tags={},serialNumber={},msg={}", MessageQueueTopicConstant.ZMN_TOPIC_PUSH, OmsMqTagConsts.PUSH_TOPIC_MESSAGE_CENTER_SYSTEM_MQ_TAGS, serialNumber, JSON.toJSONString(sendMscDIO));

        if (NumberUtil.isNotNullOrZero(sendMscDIO.getDelaySecond())) {

            long startDeliverTime = System.currentTimeMillis() + sendMscDIO.getDelaySecond() * 1000;
            sender.sendWithStartDeliverTime(MessageQueueTopicConstant.ZMN_TOPIC_PUSH, OmsMqTagConsts.PUSH_TOPIC_MESSAGE_CENTER_SYSTEM_MQ_TAGS, serialNumber, JSON.toJSONString(sendMscDIO), startDeliverTime);
        } else {
            sender.send(MessageQueueTopicConstant.ZMN_TOPIC_PUSH, OmsMqTagConsts.PUSH_TOPIC_MESSAGE_CENTER_SYSTEM_MQ_TAGS, serialNumber, JSON.toJSONString(sendMscDIO));
        }
        return serialNumber;
    }

}